<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="128px"
    >
      <el-form-item label="订单编号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="主订单号" prop="mainOrderId">
        <el-input
          v-model="queryParams.mainOrderId"
          placeholder="请输入主订单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="目的港" prop="unloadingPort">
        <el-input
          v-model="queryParams.unloadingPort"
          placeholder="请输入目的港"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['business:cargo-info:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <el-tabs type="border-card" @tab-click="tabClick" v-model="activeTab">
    <el-tab-pane name="1" :label="'待入仓('+sumaryArr[0]+')'"/>
    <el-tab-pane name="2" :label="'待包装('+sumaryArr[1]+')'"/>
    <el-tab-pane name="3" :label="'待出仓('+sumaryArr[2]+')'"/>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="客户公司" align="center" prop="companyName" />
      <el-table-column label="运单号" align="center" prop="mainOrderId" width="150" />
      <el-table-column label="订单号" align="center" prop="orderId" width="100" />
      <el-table-column label="目的港" align="center" prop="destinationCityName" />
      <el-table-column label="件数" align="center" prop="warehouseNumber" width="60"/>
      <el-table-column label="计划" align="center" prop="planStatus" :formatter="planStatusFormat" width="150"/>
      <el-table-column label="审核" v-if="activeTab=='3' || activeTab=='2'" align="center" prop="packStatus" :formatter="packStatusFormat" width="150"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            v-show="activeTab == '1'"
            type="primary"
            @click="rucang(scope.row.id)"
            v-hasPermi="['business:cargo-info:receipt']"
          >
            入仓
          </el-button>
          <el-button
            link
            v-show="activeTab == '2'"
            type="primary"
            @click="baozhuang(scope.row.id)"
            v-hasPermi="['business:cargo-info:pack']"
          >
            包装
          </el-button>
          <el-button
            link
            v-show="activeTab == '3' && scope.row.planStatus==2"
            type="primary"
            @click="chuku(scope.row.id)"
            v-hasPermi="['business:cargo-info:shipment']"
          >
            出仓
          </el-button>
          <el-button
            link
            v-show="activeTab == '3' && scope.row.planStatus!=2"
            type="primary"
            @click="viewOrder(scope.row.id)"
            v-hasPermi="['business:cargo-info:query']"
          >
            订单信息
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </el-tabs>
    

  <!-- 表单弹窗：添加/修改 -->
  <ReceiptView ref="ReceiptViewRef"/>
  <ReceiptForm ref="ReceiptFormRef" @success="callbackFunc"/>
  <PackForm ref="PackFormRef" @success="callbackFunc"/>
  <ShipmentForm ref="ShipmentFormRef" @success="callbackFunc"/>
</template>

<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import download from '@/utils/download'
import { CargoInfoApi, CargoInfoVO } from '@/api/business/cargoinfo'
import CargoInfoView from '../cargoinfo/CargoInfoView.vue'
import ReceiptForm from './ReceiptForm.vue'
import ReceiptView from './receipView.vue'
import PackForm from './PackForm.vue'
import ShipmentForm from './shipmentForm.vue'
import { dateFormatter } from '@/utils/formatTime'
import {useUserStore} from '@/store/modules/user'

/** 货物资料 列表 */
defineOptions({ name: 'ReceoptIndex' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const userStore = useUserStore();


const loading = ref(true) // 列表的加载中
const list = ref<CargoInfoVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  orderId: undefined,
  mainOrderId: undefined,
  unloadingPort: undefined,
  status: 1,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const planStatusFormat = (row, index, cell)=>{
  if(cell == 0){
    return "";
  }else if(cell == 1){
    return '待下发计划'
  }else if(cell == 2){
    return "计划中"
  }else if(cell == 3){
    return "计划完成"
  }else{
    return "";
  }
}

const packStatusFormat = (row, index, cell)=>{
  if(row.status==3 && cell == 0){
    return "待审核";
  }else if(row.status==3 && cell == 1){
    return "审核成功"
  }else if(row.status==2 && cell == -1){
    return "审核失败"
  }else if(row.status==2 && row.safetyCheck == -1){
    return "报关退回"
  }else{
    return "";
  }
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  
  try {
    const data = await CargoInfoApi.getCargoInfoPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const callbackFunc = async () => {
  sumaryInfo();
  getList();
}

const activeTab = ref("1");
const tabClick = (a)=>{
  if(queryParams.status == Number(a.paneName)){
    return;
  }else{
    queryParams.status = Number(a.paneName)
    handleQuery();
  }
}

const sumaryArr = ref([0, 0 , 0, 0, 0, 0, 0, 0])
const sumaryInfo = async () => {
  sumaryArr.value = [0, 0 , 0, 0, 0, 0, 0, 0];
    const data = await CargoInfoApi.sumaryCargoInfo({leStatus:3, geStatus:1})
    for(let i = 0; i < data.length; i++){
      let d = data[i];
      sumaryArr.value[d.status-1] = d.num;
    }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const ReceiptViewRef = ref();
const viewOrder = (id:number)=>{
  ReceiptViewRef.value.open(id);
}

const ReceiptFormRef = ref();
const rucang = (id:number)=>{
  ReceiptFormRef.value.open(id);
}

const PackFormRef = ref();
const baozhuang = (id:number)=>{
  PackFormRef.value.open(id);
}

const ShipmentFormRef = ref();
const chuku = (id)=>{
  ShipmentFormRef.value.open(id);
}

/** 初始化 **/
onMounted(() => {
  sumaryInfo();
  getList()
})
</script>
